package com.kfm.pm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo01 {

    public static void main(String[] args) {
        Connection conn = null;
        Statement statement = null;
        try {
            // 注册数据库驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            String url = "jdbc:mysql://localhost:3306/emp";
            // 获取数据库连接对象
            conn = DriverManager.getConnection(url, "root", "root");
            // 编写 sql 语句
            String sql = "insert into dept values(50, '销售', '西安')";
            // 获取执行 sql 的对象
            statement = conn.createStatement();
            // 执行 sql  insert / delete / update  --> executeUpdate  select --> executeQuery()
            int i = statement.executeUpdate(sql);
            // 判断执行结果
            if (i > 0) {
                System.out.println("成功");
            } else {
                System.out.println("失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭资源
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
